﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
using WPFExcelReport;
namespace WPFExcelReport
{
    using zl_u8 = Byte;
    using zl_u16 = System.UInt16;
    using zl_u32 = System.UInt32;
    using zl_s8 = SByte;
    using zl_s16 = System.Int16;
    using zl_s32 = System.Int32;
    using zl_bool = System.Int32;
    using zl_boolean = System.UInt32;
    using zl_char = System.SByte;
    using zl_int = System.Int32;
    using IP_ADDR = System.UInt32;
    using ZLDevHandler = System.UInt32;
    public partial class TZLANDEV
    {
        int result = -1;
        SDevParam pParam;
        int ZLANCount = 0;
        zl_bool zlbl = -1;
        Dictionary<object, object> ZLPLC = new Dictionary<object, object>();
        ZLDevHandler[] hDevs = new ZLDevHandler[256];
        public TZLANDEV()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_ZLAN();
            this.DataContext = MV;
            InitializeComponent();


        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
          
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            LOADALL();
            base.Window_Loaded(sender, e);
            ServerPortCommand(null, null);
            SearchDevCommand(null, null);
            GetDevParamCommand(null, null);

        }
        public void LOADALL()
        { 
            dwData = TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            SResult = TMV.QICC_ZLAN03(dwData);
            MergeData(SResult, false);
        }
        private void GetVerCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                result = ZLAN.GetVer();
            }
            catch (Exception ex)
            {
            }

        }
        /// <summary>
        /// ServerPort  [in] 运行该设备管理函数库的计算机的监听端口。处于 TCP Client 的设备 将向该端口发起连接。如果需要与处于 TCP Client 的设备通信则需要将 ServerPort 设置为与设备参数（如下图所示）的目的端口一样的值。由于 4196 是 ZLVirCom 的默认监听端口，使用 4196 时请先关闭 ZLVirCom 程 序，或停止 ZLVirCom 的服务。  
        //返回值 TRUE/FALSE，表示初始化是否成功。初始化失败的原因可能是 ServerPort 的端口已经被其他程序占用，例如 ZLVirCOM 正在运行时会占用 4196 端 口。  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ServerPortCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                UInt16 port = Convert.ToUInt16(txtPort.Text);
                zlbl = ZLAN.Init(port);
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }
        //退出前调用该函数。 
        private void ExitCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                ZLAN.Exit();
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

        /// </summary>
        /// <param name="sender"></param>
        ///  搜索网络中所有联网的卓岚联网设备。 
        /// hDev  [out] ZLDevHandler 类型的数组。该数组由调用者分配，数组最小长度建 议为，ZLDM_HANDLER_ARRAY_MIN_SIZE，函数返回时数组内容为 搜索到的设备的句柄，每一个设备有一个句柄。 
        /// hDevSize  [in] hDev 数组长度，该长度由调用者分配内存的时候决定。建议 hDevSize 大于 256，可以根据用户的 ZLSN 设备数量来调整。 
        /// 返回值 写入 hDev 的句柄数量，当 hDevSize 大于等于联网设备数量时，返回值就 是找到的设备的数量；当 hDevSize 小于联网设备数量时，返回值为 hDevSize。 
        /// 开始搜索网络中所有联网的 ZLSN 设备，包括物理子网中所有 ZLSN 设备和 用户指定 IP 的设备。
        /// 搜索结果会暂时保存起来，但是一旦有新的设备连接到网 络中，或者有设备退出网络，那么需要重新搜索以获得最新的设备列表。 
        /// <param name="e"></param>
        private void SearchDevCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                UInt16 hDevSize = 256 * 2;
                ZLANCount = ZLAN.StartSearchDev(hDevs, hDevSize);
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }
        private void GetDevParamCommand(object sender, RoutedEventArgs e)
        {
            try
            {

                ZLDevHandler hDev = 0;
                UInt16 hDevSize = 256 * 2;
                ZLPLC.Clear();
                TBMain.Clear();
                for (int i = 0; i < ZLANCount; i++)
                {
                    pParam = new SDevParam();
                    hDev = hDevs[i];
                    zlbl = ZLAN.GetDevParam(hDev, ref pParam);
                    string mac = TMV.NEWZLANDEV(hDev,pParam);
                    ZLPLC.Add(mac, pParam);
                }

            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }
        private void SetDevParamCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                SDevParam dev = (SDevParam)ZLPLC[TMV.DEVID];
                 dev.DevName = Helper.STR2CHAR(TMV.DEVNAME);
                dev.DestName = Helper.STR2CHAR(TMV.SERVERIP);
                dev.LocalIP = System.Convert.ToUInt32(Helper.IpToInt(TMV.LOCALIP));
                dev.NetMask = System.Convert.ToUInt32(Helper.IpToInt(TMV.NETMASK));
                dev.GateWay = System.Convert.ToUInt32(Helper.IpToInt(TMV.GATEWAY));
                dev.DnsServerIP = System.Convert.ToUInt32(Helper.IpToInt(TMV.DNSSERVERIP));
                dev.UDPGroupIP = System.Convert.ToUInt32(Helper.IpToInt(TMV.UDPGROUPIP));
                dev.ResourceIP = System.Convert.ToUInt32(Helper.IpToInt(TMV.RESOURCEIP));
                ZLDevHandler hDev =System.Convert.ToUInt32(TMV.HDEV);
                SDevParam nDev = TMV.COPYDEV(dev);
                ZLAN.SetDevParam(hDev, nDev);
                TMV.NEWZLANDEV(hDev, nDev);
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

        private void DevOpenParamCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                SDevParam dev = (SDevParam)ZLPLC[TMV.DEVID]; 
                ZLDevHandler hDev = System.Convert.ToUInt32(TMV.HDEV);
                UInt32 mode = 0;
                mode = ZLAN.ZLDM_FAST_SEND;
                mode |= ZLAN.ZLDM_CALL_MESSAGE;
                zlbl= ZLAN.DevOpen(hDev, mode, 0);
                
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

        

    }
}
